import datetime
import os
import sys

import pandas as pd

sys.path.append(os.path.dirname(os.path.split(os.getcwd())[0] + r"\ "))
import Global_param as g
from UserStruct import *
import threading


# 策略2
class strategy2(object):

    def __init__(self):
        # 订阅的合约
        self.subID = ['m2209', 'c2209', 'SA209']
        self.specific_strategy_map = {}
        for instrumentID in self.subID:
            self.specific_strategy_map[instrumentID] = strategy2.specific_strategy(instrumentID)
        # 策略编号
        self.strategyID = 2

    class specific_strategy(object):
        def __init__(self, instrumentID):
            # 策略数据
            # self.market_data = MarketData()
            self.market_data = None
            # 策略数据锁
            self.market_data_lock = threading.Lock()
            # 策略合约名称
            self.instrumentID = None

            self.save_to_csv = pd.DataFrame(columns=['最后修改时间', '最后修改毫秒', '合约名称', '最新价格', '现在时间'])

        def onQuote(self):
            # self.market_data_lock.acquire()

            # print('''策略{}\n订阅合约名称：{}\n最新价格：{}\n最后修改时间：{}\n最后修改毫秒：{}\n现在时间：{}\n
            #         '''.format(self.strategyID, self.market_data.InstrumentID, self.market_data.LastPrice,
            #                    self.market_data.UpdateTime, self.market_data.UpdateMillisec,
            #                    datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")))

            # print('策略{}, 订阅合约名称：{}'.format(self.strategyID, self.market_data.InstrumentID))

            data_list = [self.market_data.UpdateTime, self.market_data.UpdateMillisec, self.market_data.InstrumentID,
                         self.market_data.LastPrice,
                         datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")]
            self.save_to_csv = pd.concat([self.save_to_csv, pd.DataFrame([data_list, ], columns=['最后修改时间', '最后修改毫秒', '合约名称', '最新价格', '现在时间'])], ignore_index=True)
            self.save_to_csv.to_csv('./实时数据/{}.csv'.format(self.market_data.InstrumentID))

            self.market_data_lock.release()
